def print_rules(bit):
    rules = []

    # Bit Toggle
    rules.append((
        "// REG ^= (1 << %d)" % bit,
        "replace restart {",
        "    ld a, %1",
        "    xor a, #%s" % format((1 << bit) & 0xff, '#04x'),
        "    ld %1, a",
        "} by {",
        "    bcpl %%1, #%d ; peephole replaced xor by bcpl." % bit,
        "} if notUsed('a')"))

    # Bit Set
    rules.append((
        "// REG |= (1 << %d)" % bit,
        "replace restart {",
        "    ld  a, %1",
        "    or  a, #%s" % format((1 << bit) & 0xff, '#04x'),
        "    ld %1, a",
        "} by {",
        "    bset %%1, #%d ; peephole replaced or by bset." % bit,
        "} if notUsed('a')"))

    # Bit Reset
    rules.append((
        "// REG &= ~(1 << %d)" % bit,
        "replace restart {",
        "    ld  a, %1",
        "    and a, #%s" % format(~(1 << bit) & 0xff, '#04x'),
        "    ld %1, a",
        "} by {",
        "    bres %%1, #%d ; peephole replaced and by bres." % bit,
        "} if notUsed('a')"))

    for r in rules:
        print ('\n'.join(r) + '\n')

print('// Extra rules generated by rules_gen.py')
for i in range(8):
    print_rules(i)
